package com.awesome.spring.redis.Controller;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: awesome-spring
 * @description:
 * @author: Alex Wu
 * @createDate: 2025-02-27 16:05
 **/

@RestController
@RequestMapping("/redis")
public class HelloController {

    //注意：这里@Autowired是报错的，因为@Autowired按照类名注入的
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @GetMapping("/{name}")
    public Boolean saveValue(@PathVariable("name") String name) {
        redisTemplate.opsForValue().set("user_name", name);
        System.out.println("缓存设置成功");
        String value = (String) redisTemplate.opsForValue().get("user_name");
        System.out.println(value);
        return true;
    }

    @GetMapping("")
    public String hello() {
        return "success";
    }

}
